﻿using System;
using BLL;
using BLL.Access;
using DevExpress.Web;

namespace Web.Pages.QuShui.DiXiaS._3.JiHuaGL.KaiCaiJH
{
    public partial class KAICAIJHXD : BasePage
    {
        #region 属性

        #endregion

        #region 变量

        /// <summary>
        ///     存储过程执行类
        /// </summary>
        private readonly SpHelper _SpHelper = new SpHelper();

        /// <summary>
        ///     操作日志BLL
        /// </summary>
        private SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        /// <summary>
        ///     参数bll
        /// </summary>
        private readonly QS_CANSHUBBLL _CANSHUBLL = new QS_CANSHUBBLL();

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack) InitData();
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var year = "";
            var year1 = "";
            var year2 = "";
            _SpHelper.P_QSJH_MAXYEAR("QS_JH_NIANJIH", out year1);
            var dt = _CANSHUBLL.SelectQS_CANSHUBsDynamic("i_jlzt=0 and S_MINGCHENG=\'WaterYear\' or S_MIAOSHU=\'水量年\'",
                null);
            year2 = dt.Rows.Count > 0 ? dt.Rows[0]["S_ZHI"].ToString() : "";
            if (year1 == "" || year1 == null)
                year = year2;
            else
                year = (Convert.ToInt32(year1) + 1).ToString();
            if (Convert.ToInt32(year) <= Convert.ToInt32(year2) + 1)
                txtYear.Text = year;
            else
                txtYear.Text = "";
        }

        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var KCJHBLL = new QS_JH_KAICAIJHBBLL();
            var year = Convert.ToInt32(txtYear.Text.Trim());
            var msg = "";
            if (e.Parameter == "yz1")
            {
                var dt = KCJHBLL.SelectQS_JH_KAICAIJHBsDynamic(
                    string.Format("I_Y={0} and i_jlzt=0", txtYear.Text.Trim()), null);
                if (dt.Rows.Count > 0)
                    msg = "true";
                else
                    msg = "false";
                e.Result = e.Parameter + "|0|" + msg;
            }
            else if (e.Parameter == "yz2")
            {
                //根据sname 是否 矿泉井，以及年份来判断是否用户都已经制定计划
                var result = "";
                _SpHelper.P_QSJH_JIHUAGLXD("矿泉井", year.ToString(), out result);
                msg = result == "0" ? "true" : "false";
                e.Result = e.Parameter + "|0|" + msg;
            }
            else if (e.Parameter == "xdjh")
            {
                //下达采灌计划
                _SpHelper.P_QSJH_XIADAJH(4, year, out msg); //1 下达成功
                e.Result = e.Parameter + "|0|" + msg;
            }
        }

        #endregion

        #region 方法

        #endregion
    }
}